export default {
  data() {
    return {
      module   : 'users',
      data     : [],
      dataBox  : [],
      total    : 0,
      page     : 1,
      rows     : 5,
      modalForm: false,
      ids      : [],
      myUrl:this.GLO.myUrl
    };
  },
  methods: {
    handleSubmit(name) {
      this.$refs[name].validate(valid => {
        if (valid) {
          //this.formValidate = Object.assign(this.formValidate,{sex: this.formValidate.sex == 'true'})

          if (this.formValidate._id) {
            this.axios({
              url: `${this.myUrl}${this.module}/data/${
                this.formValidate._id
              }`,
              method: 'put',
              data  : this.formValidate
            }).then(res => {
              this.modalForm = false;
              this.getData();
              this.$data.formValidate = this.$options.data().formValidate;
            });
          } else {
            this.axios({
              url   : `${this.myUrl}${this.module}/data`,
              method: 'post',
              data  : this.formValidate
            }).then(res => {
              this.modalForm = false;
              this.getData();
            });
          }
        } else {
          this.$Message.error('Fail!');
        }
      });
    },
    handleReset(name) {
      this.$refs[name].resetFields();
    },
    edit(id) {
      // 请示数据，打开对话框，显示表单的数据，进行提交
      this.axios({
        url   : `${this.myUrl}${this.module}/data/${id}`,
        method: 'get'
      }).then(res => {
        this.formValidate = res.data;
        this.modalForm    = true;
      });
    },
    remove(id) {
      this.$Modal.confirm({
        title  : '确认操作',
        content: '<p>你确认删除该记录吗?</p>',
        onOk   : () => {
          this.axios({
            url   : `${this.myUrl}${this.module}/data/${id}`,
            method: 'delete'
          }).then(res => {
            this.getData();
          });
        },
        onCancel: () => {
          this.$Message.info('Clicked cancel');
        }
      });
    },
    getData() {
      this.axios({
        url   : `${this.myUrl}${this.module}/list`,
        method: 'post',
        data  : {
          page: this.page,
          rows: this.rows
        }
      }).then(res => {
        this.total   = res.data.total;
        this.data    = res.data.rows;
        this.dataBox = res.data.rows;
      });
    },
    searchAll(){
      this.getData()
    },
    onChangePage(page) {
      this.page = page;
      this.getData()
    },
    onPageSizeChange(rows) {
      this.rows = rows;
      this.getData()
    },

    onSelect(selections) {
      var ids = [];
      for (let i = 0; i < selections.length; i++) {
        ids.push(selections[i]._id);
      }
      this.ids = ids.toString();
      console.log(ids);
    },
    removes() {
      this.$Modal.confirm({
        title  : '确认操作',
        content: '<p>你确认删除该记录吗?</p>',
        onOk   : () => {
          this.axios({
            url   : `${this.myUrl}${this.module}/data/removes`,
            method: 'post',
            data  : {
              ids: this.ids
            }
          }).then(res => {
            this.getData();
          });
        },
        onCancel: () => {
          this.$Message.info('Clicked cancel');
        }
      });
    },
    onSearch(searchText) {
      var dataList = this.dataBox;
      var keyWords = searchText.split(" ");

      function matchKeyWord(field) {
        if (!field) {
          return false;
        }
        for (var i in keyWords) {
          var kw = keyWords[i];
          if (!kw || kw.trim() == "") {
            continue;
          }
          if (new RegExp(kw).test(field + "")) {
            return true;
          }
        }
      }
      var newDataList = [];
      for (var i = 0; i < dataList.length; i++) {
        var row = dataList[i];
        for (var k in row) {
          var txt = row[k];
          if (k == 'sex') {
            if (row[k]) {
              txt = '男'
            } else {
              txt = '女'
            }
          }
          if (matchKeyWord(txt)) {
            newDataList.push(row);
            break;
          }
        }
      }
      this.data = newDataList;
    }
  },
  mounted() {
    this.getData();
  }
};





